package org.jeecg.modules.basic.repair.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.basic.repair.entity.LwgyDepreRecord;
import org.jeecg.modules.basic.repair.entity.LwgyFixed;
import org.jeecg.modules.basic.repair.mapper.LwgyDepreRecordMapper;
import org.jeecg.modules.basic.repair.mapper.LwgyFixedMapper;
import org.jeecg.modules.basic.repair.service.ILwgyFixedService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;


/**
 * @Description: 固定资产
 * @Author: jeecg-boot
 * @Date:   2023-02-08
 * @Version: V1.0
 */
@Service
public class LwgyFixedServiceImpl extends ServiceImpl<LwgyFixedMapper, LwgyFixed> implements ILwgyFixedService {

    @Resource
    private LwgyFixedMapper lwgyFixedMapper;

    @Resource
    private LwgyDepreRecordMapper lwgyDepreRecordMapper;

    @Override
    public void saveMain(LwgyFixed lwgyFixed, List<LwgyDepreRecord> lwgyDepreRecordList) {
        lwgyFixedMapper.insert(lwgyFixed);
        if(lwgyDepreRecordList!=null && lwgyDepreRecordList.size()>0) {
            for(LwgyDepreRecord entity:lwgyDepreRecordList) {
                //外键设置
                entity.setAssetsId(lwgyFixed.getId());
                lwgyDepreRecordMapper.insert(entity);
            }
        }
    }

    @Override
    public void updateMain(LwgyFixed lwgyFixed, List<LwgyDepreRecord> lwgyDepreRecordList) {
        lwgyFixedMapper.updateById(lwgyFixed);

        //1.先删除子表数据
        lwgyDepreRecordMapper.deleteByMainId(lwgyFixed.getId());

        //2.子表数据重新插入
        if(lwgyDepreRecordList!=null && lwgyDepreRecordList.size()>0) {
            for(LwgyDepreRecord entity:lwgyDepreRecordList) {
                //外键设置
                entity.setAssetsId(lwgyFixed.getId());
                lwgyDepreRecordMapper.insert(entity);
            }
        }

    }

    @Override
    public void delMain(String id) {
        lwgyDepreRecordMapper.deleteByMainId(id);
        lwgyFixedMapper.deleteById(id);
    }

    @Override
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for(Serializable id:idList) {
            lwgyDepreRecordMapper.deleteByMainId(id.toString());
            lwgyFixedMapper.deleteById(id);
        }
    }
}
